package com.sh.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.sh.common.idempotent.annotation.RepeatSubmit;
import com.sh.common.log.annotation.Log;
import com.sh.common.web.core.BaseController;
import com.sh.common.mybatis.core.page.PageQuery;
import com.sh.common.core.domain.R;
import com.sh.common.core.validate.AddGroup;
import com.sh.common.core.validate.EditGroup;
import com.sh.common.log.enums.BusinessType;
import com.sh.common.excel.utils.ExcelUtil;
import com.sh.business.domain.vo.BannerVo;
import com.sh.business.domain.bo.BannerBo;
import com.sh.business.service.IBannerService;
import com.sh.common.mybatis.core.page.TableDataInfo;

/**
 * 后台管理-banner
 *
 * @author ruoyi
 * @date 2024-10-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/banner")
public class BannerController extends BaseController {

    private final IBannerService bannerService;

    /**
     * 查询banner列表
     */
    @SaCheckPermission("business:banner:list")
    @GetMapping("/list")
    public TableDataInfo<BannerVo> list(BannerBo bo, PageQuery pageQuery) {
        return bannerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出banner列表
     */
    @SaCheckPermission("business:banner:export")
    @Log(title = "banner", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BannerBo bo, HttpServletResponse response) {
        List<BannerVo> list = bannerService.queryList(bo);
        ExcelUtil.exportExcel(list, "banner", BannerVo.class, response);
    }

    /**
     * 获取banner详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:banner:query")
    @GetMapping("/{id}")
    public R<BannerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(bannerService.queryById(id));
    }

    /**
     * 新增banner
     */
    @SaCheckPermission("business:banner:add")
    @Log(title = "banner", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BannerBo bo) {
        return toAjax(bannerService.insertByBo(bo));
    }

    /**
     * 修改banner
     */
    @SaCheckPermission("business:banner:edit")
    @Log(title = "banner", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BannerBo bo) {
        return toAjax(bannerService.updateByBo(bo));
    }

    /**
     * 删除banner
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:banner:remove")
    @Log(title = "banner", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(bannerService.deleteWithValidByIds(List.of(ids), true));
    }
}
